//
//  VCOfAddContact.m
//  17-HomeWork(通讯录)
//
//  Created by ParkourH on 16/8/6.
//  Copyright © 2016年 ParkourH. All rights reserved.
//

#import "VCOfAddContact.h"

@interface VCOfAddContact () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *barButtonOfSave;
@property (weak, nonatomic) IBOutlet UITextField *textFieldOfContactName;
@property (weak, nonatomic) IBOutlet UITextField *textFieldOfContactAge;
@property (weak, nonatomic) IBOutlet UITextField *textFieldOfContactPhoneNumber;
@property (weak, nonatomic) IBOutlet UITextField *textFieldOfContactSex;
@property (weak, nonatomic) IBOutlet UITextView *textViewOfContact;
@property (weak, nonatomic) IBOutlet UIImageView *imageOfContact;

@end

@implementation VCOfAddContact

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.barButtonOfSave setAction:@selector(saveContact)];
    
    self.view.backgroundColor = [UIColor redColor];
    [self pickImage];
}
/** 添加联系人保存按钮 */
- (void)saveContact {

    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:self.imageOfContact.image forKey:@"image"];
    [dic setValue:self.textFieldOfContactName.text forKey:@"name"];
    [dic setValue:self.textFieldOfContactAge.text forKey:@"age"];
    [dic setValue:self.textFieldOfContactSex.text forKey:@"gender"];
    [dic setValue:self.textFieldOfContactPhoneNumber.text forKey:@"phoneNumber"];
    [dic setValue:self.textViewOfContact forKey:@"textView"];
    
    Contact *contact = [[Contact alloc] init];
    
    [contact setValuesForKeysWithDictionary:dic];
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(addContact:)]) {
        
        [self.delegate addContact:contact];
    }
    
    
}

/** 选择照片 */
- (void)pickImage {

    self.imageOfContact.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pickPhoto)];
    [self.imageOfContact addGestureRecognizer:tap];
}

- (void)pickPhoto {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    
    [self presentViewController:picker animated:YES completion:^{
        
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo {

    self.imageOfContact.image = image;
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
